package model.builders;

import java.util.Random;

import model.Graph;

public class GraphBuilder {

    private static final int infinity = Integer.MAX_VALUE;
    private static final Random rand = new Random();
    private int[][] t;
    private final int size;

    public GraphBuilder(int size) {
        if (size < 1) {
            size = 1;
        }
        t = new int[size][];
        for (int i = 0; i < size; i++) {
            t[i] = new int[i + 1];
        }
        this.size = size;
    }


    public void set(int i, int j, int value) {
        if (j > i)
            t[j][i] = value;
        else
            t[i][j] = value;
    }

    public int get(int i, int j) {
        if (j > i)
            return t[j][i];
        else
            return t[i][j];
    }

    public int getSize() {
        return size;
    }

    public GraphBuilder fill(int range) {
        for (int i = 0; i < size; i++) {
            for (int j = 0; j <= i; j++) {
                if (i == j) {
                    set(i, j, infinity);
                } else {
                    set(i, j, rand.nextInt(range) + 1);
                }
            }
        }
        return this;
    }
    
    public Graph build() {
        return new Graph(t, size);
    }
}
